﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.Utils;
using System.Windows.Browser;


namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 初始数据主表  
    /// </summary>
    /// Title:ZMCMG0
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张坤
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张坤   [创建]
    ///
    public partial class ZMCMG0 : EFViewCmes
    {
        public ZMCMG0()
        {
            InitializeComponent();
        }
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {

            var selectedItem = this.grid.SelectedItem;
            if (selectedItem != null)
            {
                this.levelTab.Visibility = Visibility.Visible;
                var fdDepotCode = selectedItem.GetType().GetProperty("fdDepotCode").GetValue(this.grid.SelectedItem, null);
                var fdDepotName = selectedItem.GetType().GetProperty("fdDepotName").GetValue(this.grid.SelectedItem, null);
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdDepotCode", fdDepotCode.ToString());
                args.Add("fdDepotName", fdDepotName.ToString());
                if (this.LevelBase.IsSelected)
                {
                    this.LevelBase.LoadView("ZMCMG1", args);
                }
                if (this.LevelMaterialBase.IsSelected)
                {
                    this.LevelMaterialBase.LoadView("ZMCMG2", args);
                }
            }
            else
            {
                this.levelTab.Visibility = Visibility.Collapsed;
            }
        }
        /// <summary>
        /// 页签改变加载不同的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab tab = sender as EFTab;
            if (tab.SelectedItem != null && this.grid.SelectedItem != null)
            {
                
                if (tab.SelectedItem.Equals(this.LevelMaterialBase))
                {
                    var fdDepotCode = this.grid.SelectedItem.GetType().GetProperty("fdDepotCode").GetValue(this.grid.SelectedItem, null);
                    var fdDepotName = this.grid.SelectedItem.GetType().GetProperty("fdDepotName").GetValue(this.grid.SelectedItem, null);
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDepotCode", fdDepotCode.ToString());
                    args.Add("fdDepotName", fdDepotName.ToString());
                    this.LevelMaterialBase.LoadView("ZMCMG2", args);
                }
                else if (tab.SelectedItem.Equals(this.LevelBase))
                {
                    var fdDepotCode = this.grid.SelectedItem.GetType().GetProperty("fdDepotCode").GetValue(this.grid.SelectedItem, null);
                    var fdDepotName = this.grid.SelectedItem.GetType().GetProperty("fdDepotName").GetValue(this.grid.SelectedItem, null);
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdDepotCode", fdDepotCode.ToString());
                    args.Add("fdDepotName", fdDepotName.ToString());
                    this.LevelBase.LoadView("ZMCMG1", args);
                }
            }
        }

        /// <summary>
        /// 主表打印操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行导出！", true);
                return;
            }
            else
            {
                if (count > 1)
                {
                    this.OpenEFWindowAlert("只能选择一条记录进行导出！", true);
                    return;
                }
            }
            String depotCode = "'" + this.grid.GetCheckedEIRows()[0]["fdDepotCode"].ToString() + "'";
            Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=export2Level&depotCode=" + depotCode);
            HtmlPage.Window.Navigate(uri, "_blank");
            base.TempButtonOpen(true);//按钮生效
        }

    }
}
